package com.xiaoxu.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.Customer;
import com.xiaoxu.rental.service.CustomerService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/rental/customer")
@SuppressWarnings("all")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @ApiOperation("新增客户")
    @PostMapping
    public Result save(@RequestBody Customer customer){
        return customerService.save(customer) ? Result.success() :
                Result.fail();
    }

    @PutMapping
    public Result update(@RequestBody Customer customer) {
        customer.setUpdateTime(LocalDateTime.now());
        return customerService.updateById(customer) ? Result.success() :
                Result.fail();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        return customerService.delete(ids) ? Result.success() :
                Result.fail();
    }

    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start, @PathVariable int size,
                         @RequestBody Customer customer){
        Page<Customer> page = new Page<>(start, size);
        return Result.success(customerService.searchByPage(page, customer));
    }

    @ApiOperation(value = "根据用户电话号码,查询用户信息")
    @PostMapping("/selectCustomerByTel")
    public Result selectCustomerByTel(@RequestBody Customer customer){
        return Result.success(customerService.selectCustomerByTel(customer));
    }

}
